package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Bean.Course;
import com.example.demo.Bean.Result;
import com.example.demo.Bean.StudentClass;
import com.example.demo.Bean.TeacherClass;
import com.example.demo.Mapper.CourseMapper;
import com.example.demo.Mapper.StudentClassMapper;
import com.example.demo.Mapper.StudentMapper;
import com.example.demo.Mapper.TeacherClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    TeacherClassMapper teacherClassMapper;

    @Autowired
    StudentClassMapper studentClassMapper;

    @Autowired
    StudentMapper studentMapper;

    public Result getAllCourse(String teacherUid) throws Exception{
        Result result = null;
        List<String> ids = new ArrayList<>();
        List<TeacherClass> teacherClasses = teacherClassMapper.selectList(new QueryWrapper<TeacherClass>().eq("teacher_uid",teacherUid));
        for(int i = 0 ; i < teacherClasses.size() ; i++){
            ids.add(teacherClasses.get(i).getCourseId());
        }
        if(ids.size() == 0){
            return new Result(200,"暂无课程！");
        }
        List<Course> courses = courseMapper.selectBatchIds(ids);
        result = new Result(200,courses);
        return result;
    }

    public Result getCourseDetail(String courseId) throws Exception{
        Result result = null;
        Course course = courseMapper.selectById(courseId);
        if (course != null) {
            result = new Result(200, course);
        } else {
            result = new Result(403, "获取失败！");
        }
        return result;
    }

    public Result modifyCourseDetail(Course course) throws Exception{
        Result result = null;
        Integer r = courseMapper.updateById(course);
        if (r >= 1) {
            result = new Result(200, "修改成功");
        } else {
            result = new Result(403, "修改失败！课程编号暂不支持修改");
        }
        return result;
    }

    public Result addCourse(Course course,String teacherUid) throws Exception{
        Result result = null;
        Course c = courseMapper.selectOne(new QueryWrapper<Course>().eq("course_id",course.getCourseId()));
        if(c!=null){
            result = new Result(403,"课程编号已存在！");
            return result;
        }
        Integer r = courseMapper.insert(course);
        if (r >= 1) {
            TeacherClass teacherClass = new TeacherClass();
            teacherClass.setCourseId(course.getCourseId());
            teacherClass.setTeacherUid(teacherUid);
            teacherClassMapper.insert(teacherClass);
            result = new Result(200, "添加成功！");
        } else {
            result = new Result(403, "操作失败！");
        }
        return result;
    }

    public Result searchCourse(String name,String teacherUid,Integer pageNum,Integer pageSize) throws Exception{
        Result result = null;
            Map<String,Object> res = new HashMap<>();
            List<TeacherClass> teacherClasses = teacherClassMapper.selectList(new QueryWrapper<TeacherClass>().eq("teacher_uid",teacherUid));
            List<String> ids = new ArrayList<>();
            for (TeacherClass teacherClass : teacherClasses) {
                ids.add(teacherClass.getCourseId());
            }
            if(ids.size() == 0){
                return new Result(200, "未找到相关课程！");
            }
            Page<Course> page = new Page<>(pageNum,pageSize);
            page = courseMapper.selectPage(page,new QueryWrapper<Course>().like("name",name).and(wrapper -> {wrapper.in("course_id",ids);}));
            List<Course> list = page.getRecords();
            res.put("page",pageNum);
            res.put("size",pageSize);
            Integer total = courseMapper.selectList(new QueryWrapper<Course>().like("name",name).and(wrapper -> {wrapper.in("course_id",ids);})).size();
            res.put("total",total);
            res.put("totalPage",total%pageSize>=1 ? total/pageSize+1 : total/pageSize);
            res.put("list",list);
            if (list.size()!=0) {
                result = new Result(200, res);
            } else {
                result = new Result(200, "未找到相关课程！");
            }
        return result ;
    }

    public Result deleteCourse(String courseId,String teacherUid) throws Exception{
        Result result = null;
                List<StudentClass> studentClasses = studentClassMapper.selectList(new QueryWrapper<StudentClass>().eq("course_id", courseId));
                HashMap map = new HashMap<>();
                map.put("course_id",courseId);
                map.put("teacher_uid",teacherUid);
                teacherClassMapper.deleteByMap(map);
                for (StudentClass studentClass : studentClasses) {
                    studentMapper.deleteById(studentClass.getStudentUid());
                }
                courseMapper.deleteById(courseId);
                result = new Result(200, "删除成功！");
        return result;
    }

    public Result getPage(Integer pageNum,Integer pageSize,String teacherUid) throws Exception{
        Result result = null;
            Map<String,Object> res = new HashMap<>();
            res.put("page",pageNum);
            res.put("size",pageSize);
            Integer total = (Integer)getTotalCourseNum(teacherUid).getObj();
            res.put("total",total);
            res.put("totalPage",total%pageSize>=1 ? total/pageSize+1 : total/pageSize);
            List<TeacherClass> teacherClasses = teacherClassMapper.selectList(new QueryWrapper<TeacherClass>().eq("teacher_uid",teacherUid));
            List<String> ids = new ArrayList<>();
            for (TeacherClass teacherClass : teacherClasses) {
                ids.add(teacherClass.getCourseId());
            }
            Page<Course> page = new Page<>(pageNum, pageSize);
            page = courseMapper.selectPage(page,new QueryWrapper<Course>().in("course_id",ids));
            res.put("list",page.getRecords());
            result = new Result(200, res);
        return result;
    }

    public Result getTotalCourseNum(String teacherUid) throws Exception{
        Result result = null;
            List<Course> courses = courseMapper.selectList(null);
            System.out.println(courses);
            HashMap map = new HashMap();
            map.put("teacher_uid",teacherUid);
            Iterator<Course> iterator = courses.iterator();
            while (iterator.hasNext()) {
                Course next = iterator.next();
                map.put("course_id",next.getCourseId());
                TeacherClass t = teacherClassMapper.selectOne(new QueryWrapper<>().allEq(map));
                if(t==null){
                    iterator.remove();
                }
            }
            result = new Result(200, courses.size());
        return result;
    }

}
